angular.module('casher.controllers', ['casher.services'])
.controller('AppCtrl', function($scope,$rootScope,$location,$meteor,$state) {
	$rootScope.isEdit = false;
	$rootScope.toggleEdit=function(){ //callback
		$rootScope.isEdit=!$rootScope.isEdit;
	};
	$rootScope.isFirstLogin = !!$location.search().firstLogin;
})
.controller('HomeCtrl', function($scope, $state, SalesService) {  //, InventoryService
	var saleOrdersSubHandle = $scope.subscribe('sale_orders');
	$scope.helpers({
		saleOrders: function() {
			return SaleOrders.find({'state':'待付款'});
		},
		paidOrder: function() {
			return SaleOrders.find({'state':'已付款'});
		}
	});

})


.controller('filterController', function($scope,$rootScope) {
	$rootScope.showFilter = function() {
		$rootScope.filterShow = !$rootScope.filterShow;
	};
	$rootScope.filterDate = function(date) {
		$rootScope.filterShow = !$rootScope.filterShow;
	};
})

// 历史销售单
.controller('SaleOrderListCtrl', function($scope) {
	var saleOrdersSubHandle = $scope.subscribe('sale_orders');
	$scope.helpers({
		sale_orders: function() {
			return SaleOrders.find({});
		}
	});

})

// 销售单
.controller('SaleOrderDetailCtrl', function($scope,$rootScope,$stateParams,$state,$ionicPopup,ToastService,PopupService) {
	var saleOrdersSubHandle = $scope.subscribe('sale_orders');

	$scope.isOrderCorrect=false;
	$scope.barcodeList=[];

	$scope.$on('$ionicView.enter', function(e) {
		$scope.helpers({
			theOrder: function() {
				return SaleOrders.findOne({'_id':$stateParams._id});
			}

		});


		$scope.autorun(function(){
			if(saleOrdersSubHandle.ready() && $scope.theOrder){
				$scope.toState={
					order_id: $scope.theOrder._id,
					state:{}
				};
				$scope.theOrder.list.forEach(function(product){
					product.inventory_check=0;
					$scope.barcodeList.push(product.barcode);
				});

			}
		});
	});

	$scope.updateState = function(){
		if($scope.detail.state===0 || $scope.detail.state===5) return;

		var myPopup = $ionicPopup.show({
			template: '<ion-list>'+
					    '<ion-radio ng-model="toState.state.operation.state" ng-value="{{theState}}" ng-repeat="theState in upState">{{stateUi[theState].note}}</ion-radio>'+
					  '</ion-list>',
			cssClass: 'popup-radio',
			title: '更新采购单状态',
			scope: $scope,
			buttons: [
				{ text: '取消' },
				{ text: '确定',type: 'button-royal',
					onTap: function(e) {
						$scope.toState.state.date=moment().toDate();
						$scope.toState.state.operation.note=$scope.stateUi[$scope.toState.state.operation.state].note;
						$scope.toState.state.operator={
			        		'_id':$rootScope.currentUser._id,
			        		'name':$rootScope.currentUser.profile.name
				        };
				        // 'remark':$scope.operation.remark
						// console.log($scope.toState);

				        Meteor.call('updatePOState', $scope.toState, function(error){
							if (error) {
								PopupService.showAlert(error.reason);
							} else {
								ToastService.show('状态更新成功');
							}
						});
					}
				}
			]
		});
	};



	$scope.scanBarcode = function(index) {
		if($scope.theOrder.list[index].inventory_check<$scope.theOrder.list[index].count){
			$scope.theOrder.list[index].inventory_check++;
		}
		var totalCount = 0;

		$scope.theOrder.list.forEach(function(p){
			totalCount+=p.inventory_check;
		});

		//模拟

		if(totalCount == $scope.theOrder.total_count ) $scope.isOrderCorrect = true;

	};

	$scope.checkout = function(){
		$scope.toState.state.date=moment().toDate();
		$scope.toState.state.operation='已付款';
		$scope.toState.state.operator={
    		'_id':$rootScope.currentUser._id,
    		'name':$rootScope.currentUser.profile.name
        };

        Meteor.call('updateSOState', $scope.toState, function(error){
			if (error) {
					PopupService.showAlert(error.reason);
			} else {
				ToastService.show('结算成功');
				$state.go('app.home');
			}
		});
	};
});
